import request from "@/utils/request";
import {QueueRegCategoryModel} from "@/model/QueueRegCategoryModel";
import {QueueYjCategoryModel} from "@/model/QueueYjCategoryModel";
import {QueueRegItemModel} from "@/model/QueueRegItemModel";
import {QueueYjItemsModel} from "@/model/QueueYjItemsModel";
import {PagingModel} from "@/model/PagingModel";

export default class YjItemsApi {
    /**
     * 查询类目列表
     *
     */
    public static listYjItem(categoryId: string, pageIndex: number, pageSize: number): Promise<PagingModel<QueueYjItemsModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/yj/items/list',
                method: 'get',
                params: {
                    categoryId,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    const result = {
                        data: [],
                        pageSize: 0,
                        pageIndex: 1,
                        total: 0
                    }
                    result.data = res.rows.map(item => new QueueYjItemsModel().deserialize(item))
                    result.pageIndex = pageIndex
                    result.pageSize = pageSize
                    result.total = +res.total
                    resolve(result)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static saveYjItem(model: QueueYjItemsModel): Promise<QueueYjItemsModel> {
        return new Promise((resolve, reject) => {
            request({
                url: '/queue/yj/items/save',
                method: 'POST',
                data: model
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueYjItemsModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static updateYjItem(id: string, model: QueueYjItemsModel): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/yj/items/${id}`,
                method: 'PUT',
                data: model
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }

    public static deleteYjItem(id: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/yj/items/${id}`,
                method: 'DELETE'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject()
                }
            }, ()=> {reject()})
        })
    }
}